#include <stdio.h>

int findMaxEleIndex(int arr[], int n) {
	int index = 0;
	int max = arr[0];
	int i;

	for (i = 0; i < n; ++i) {
		if (arr[i] > max) {
			max = arr[i];
			index = i;
		}		
	}

	return index;
}
//每次找到一个最大的元素与末尾的交换


void selectionSort(int arr[], int n) {
	while (n > 1) {
		int pos = findMaxEleIndex(arr, n);
		int temp = arr[pos];
		arr[pos] = arr[n - 1];
		arr[n - 1] = temp;
		--n;
	}
}

void print(int arr[], int n) {
	int i;
	for (i = 0; i < n; ++i) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

int main() {
	int arr[5] = {2, 5, 7, 3, 1};
	printf("Before: ");
	print(arr, 5);

	selectionSort(arr, 5);

	printf("After: ");
	print(arr, 5);

	return 0;
}
